/**
 * 
 */
package com.webwork.common.dao;

import java.util.ArrayList;
import java.util.List;

import com.webwork.common.dataobject.BetweenEnum;

/**
 * 数据列
 *
 */
public final class ColumnLimit {

	private List<String> columns = new ArrayList<String>();

	private List<ColumnLimit> columnObjs = new ArrayList<ColumnLimit>();

	private List<Boolean> isQues = new ArrayList<Boolean>();

	private BetweenEnum betweenColumn;

	public ColumnLimit() {
		super();
		betweenColumn = BetweenEnum.AND;
	}

	public ColumnLimit(BetweenEnum betweenColumn) {
		super();
		this.betweenColumn = betweenColumn;
	}

	public BetweenEnum getBetweenColumn() {
		return betweenColumn;
	}

	public String getBetweenColumnStr() {
		return String.format(" %s ", betweenColumn.name());
	}

	public void setBetweenColumn(BetweenEnum betweenColumn) {
		this.betweenColumn = betweenColumn;
	}

	public void add(ColumnLimit limit) {
		columnObjs.add(limit);
	}

	public void add(String column) {
		columns.add(column);
		isQues.add(true);
	}

	public void add(String column, boolean isQue) {
		columns.add(column);
		isQues.add(isQue);
	}
	
	public String add(String column, int length, boolean isQue) {
		isQues.add(isQue);
		StringBuilder sb = new StringBuilder(column);
		if (!isQue) {
			if (length > 0) {
				sb.append("(");
				for (int i = 0; i < length; i++) {
					sb.append("?").append(",");
				}
				sb.deleteCharAt(sb.length() - 1);
				sb.append(")");
			}
		}
		columns.add(sb.toString());
		return sb.toString();
	}

	public boolean get(String column) {
		return isQues.get(columns.indexOf(column));
	}

	public List<String> getColumns() {
		return columns;
	}

	public List<ColumnLimit> getColumnObjs() {
		return columnObjs;
	}

	public boolean isEmpty() {
		return (columns.isEmpty() || isQues.isEmpty()) && columnObjs.isEmpty();
	}
	
}
